import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  // prevent vite from obscuring rust errors
  base: './',
  resolve: {
    alias: {
      '@': '/src',
      '@/views': '/src/views',
      '@/views/*': '/src/views/*',
      '@/assets': '/src/assets',
      '@/components': '/src/components',
      '@/pages': '/src/pages',
      '@/utils': '/src/utils',
      '@/hooks': '/src/hooks',
      '@/store': '/src/store',
      '@/config': '/src/config',
      '@/api': '/src/api',
      '@/router': '/src/router',
      '@/layouts': '/src/layouts',
      '@/styles': '/src/styles',
      '@/types': '/src/types',
      '@/locales': '/src/locales',
    },
  },
  clearScreen: false,
  // Tauri expects a fixed port, fail if that port is not available
  server: {
    strictPort: true,
    host: '127.0.0.1',
    port: 4090,
    // watch: {
    //   usePolling: true,
    // }
  },
  // to make use of `TAURI_PLATFORM`, `TAURI_ARCH`, `TAURI_FAMILY`,
  // `TAURI_PLATFORM_VERSION`, `TAURI_PLATFORM_TYPE` and `TAURI_DEBUG`
  // env variables
  envPrefix: ['VITE_', 'TAURI_'],

  // css预处理器
  css: {
    // postcss: {
    //   plugins: [
    //     pxToViewPort({
    //       viewportWidth: 375, // (Number) The width of the viewport.
    //       viewportHeight: 667, // (Number) The height of the viewport.
    //       unitPrecision: 5, // (Number) The decimal numbers to allow the REM units to grow to.
    //       viewportUnit: 'vw', // (String) Expected units.
    //       selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.
    //       minPixelValue: 1, // (Number) Set the minimum pixel value to replace.
    //       mediaQuery: false, // (Boolean) Allow px to be converted in media queries.
    //     }),
    //   ]
    // },
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        additionalData: `@import "${path.resolve(__dirname, './src/styles/index.less')}"; @import "${path.resolve(__dirname, './src/styles/global-color.less')}";`,
      }
    }
  },
  build: {
    // Tauri uses Chromium on Windows and WebKit on macOS and Linux
    target: process.env.TAURI_PLATFORM == 'windows' ? 'chrome105' : 'safari13',
    // don't minify for debug builds
    minify: !process.env.TAURI_DEBUG ? 'esbuild' : false,
    // 为调试构建生成源代码映射 (sourcemap)
    sourcemap: !!process.env.TAURI_DEBUG,
    outDir: './dist',
  },
})